Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 667)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.96778 12.96714 12.96652 12.96589 12.96527 12.96465 12.96403 12.96341
##   [9] 12.96278 12.96215 12.96150 12.96085 12.96018 12.95949 12.95879 12.95806
##  [17] 12.95731 12.95654 12.95574 12.95491 12.95405 12.95316 12.95223 12.95127
##  [25] 12.95026 12.94921 12.94812 12.94698 12.94579 12.94456 12.94326 12.94192
##  [33] 12.94051 12.93905 12.93753 12.93594 12.93427 12.93250 12.93065 12.92871
##  [41] 12.92670 12.92461 12.92247 12.92026 12.91800 12.91570 12.91336 12.91099
##  [49] 12.90859 12.90617 12.90373 12.90129 12.89885 12.89641 12.89398 12.89157
##  [57] 12.88919 12.88683 12.88451 12.88224 12.88001 12.87783 12.87572 12.87367
##  [65] 12.87170 12.86981 12.86800 12.86629 12.86467 12.86316 12.86177 12.86024
##  [73] 12.85836 12.85614 12.85362 12.85080 12.84773 12.84441 12.84087 12.83713
##  [81] 12.83322 12.82916 12.82497 12.82067 12.81629 12.81185 12.80737 12.80287
##  [89] 12.79839 12.79393 12.78952 12.78519 12.78096 12.77685 12.77289 12.76908
##  [97] 12.76547 12.76207 12.75890 12.75599 12.75336 12.75104 12.74903 12.74738
## [105] 12.74609 12.74520 12.74432 12.74308 12.74153 12.73971 12.73766 12.73541
## [113] 12.73302 12.73051 12.72793 12.72532 12.72272 12.72017 12.71772 12.71540
## [121] 12.71325 12.71131 12.70963 12.70824 12.70719 12.70651 12.70625 12.70645
## [129] 12.70714 12.70838 12.71019 12.71262 12.71571 12.71950 12.72403 12.73012
## [137] 12.73843 12.74874 12.76083 12.77451 12.78954 12.80573 12.82286 12.84072
## [145] 12.85909 12.87776 12.89652 12.91516 12.93346 12.95122 12.96821 12.98423
## [153] 12.99907 13.01251 13.02434 13.03435 13.04451 13.05676 13.07084 13.08648
## [161] 13.10341 13.12138 13.14010 13.15931 13.17876 13.19816 13.21726 13.23579
## [169] 13.25348 13.27007 13.28528 13.29886 13.31053 13.32003 13.32710 13.33319
## [177] 13.33989 13.34710 13.35474 13.36273 13.37096 13.37937 13.38785 13.39632
## [185] 13.40470 13.41289 13.42081 13.42837 13.43548 13.44205 13.44800 13.45324
## [193] 13.45768 13.46124 13.46382 13.46533 13.46570 13.46483 13.46263 13.45902
## [201] 13.45391 13.44721 13.43883 13.42869 13.41599 13.40028 13.38191 13.36126
## [209] 13.33871 13.31464 13.28940 13.26339 13.23698 13.21053 13.18442 13.15903
## [217] 13.13473 13.11190 13.09091 13.06887 13.04289 13.01339 12.98075 12.94539
## [225] 12.90769 12.86807 12.82691 12.78462 12.74160 12.69825 12.65496 12.61214
## [233] 12.57018 12.52949 12.49047 12.45351 12.41902 12.38739 12.35903 12.33433
## [241] 12.31125 12.28758 12.26346 12.23901 12.21437 12.18968 12.16508 12.14069
## [249] 12.11665 12.09309 12.07016 12.04798 12.02669 12.00643 11.98733 11.96952
## [257] 11.95314 11.93832 11.92520 11.91485 11.90797 11.90411 11.90283 11.90370
## [265] 11.90628 11.91012 11.91479 11.91986 11.92488 11.92941 11.93301 11.93525
## [273] 11.93569 11.93388 11.92940 11.92435 11.92102 11.91919 11.91866 11.91922
## [281] 11.92067 11.92279 11.92539 11.92826 11.93118 11.93396 11.93638 11.93824
## [289] 11.93934 11.93947 11.93841 11.93597 11.93193 11.92610 11.91955 11.91347
## [297] 11.90775 11.90231 11.89707 11.89193 11.88681 11.88162 11.87627 11.87068
## [305] 11.86474 11.85839 11.85153 11.84407 11.83592 11.82699 11.81582 11.80126
## [313] 11.78371 11.76356 11.74123 11.71710 11.69158 11.66507 11.63797 11.61068
## [321] 11.58360 11.55713 11.53167 11.50762 11.48539 11.46536 11.44795 11.43354
## [329] 11.42255 11.41194 11.39872 11.38336 11.36632 11.34807 11.32907 11.30981
## [337] 11.29073 11.27231 11.25502 11.23932 11.22567 11.21456 11.20643 11.20176
## [345] 11.20102 11.20332 11.20739 11.21311 11.22039 11.22910 11.23915 11.25042
## [353] 11.26281 11.27621 11.29050 11.30558 11.32134 11.33767 11.35446 11.37161
## [361] 11.38900 11.40653 11.42409 11.44156 11.46062 11.48282 11.50789 11.53556
## [369] 11.56558 11.59767 11.63156 11.66700 11.70371 11.74142 11.77988 11.81880
## [377] 11.85794 11.89701 11.93575 11.97390 12.01119 12.04735 12.08212 12.11522
## [385] 12.14640 12.17538 12.20190 12.22886 12.25905 12.29204 12.32739 12.36468
## [393] 12.40348 12.44335 12.48386 12.52459 12.56509 12.60494 12.64371 12.68097
## [401] 12.71628 12.74921 12.77933 12.80622 12.82943 12.84855 12.86530 12.88169
## [409] 12.89763 12.91306 12.92790 12.94209 12.95555 12.96821 12.98000 12.99085
## [417] 13.00069 13.00944 13.01705 13.02342 13.02850 13.03222 13.03354 13.03171
## [425] 13.02702 13.01978 13.01028 12.99882 12.98571 12.97124 12.95571 12.93944
## [433] 12.92271 12.90582 12.88909 12.87280 12.85726 12.84277 12.82963 12.81814
## [441] 12.80860 12.79854 12.78551 12.76979 12.75169 12.73151 12.70955 12.68610
## [449] 12.66146 12.63594 12.60983 12.58344 12.55705 12.53098 12.50551 12.48095
## [457] 12.45760 12.43576 12.41572 12.39779 12.38226 12.36943 12.35782 12.34580
## [465] 12.33348 12.32096 12.30833 12.29571 12.28319 12.27088 12.25887 12.24728
## [473] 12.23619 12.22573 12.21597 12.20704 12.19902 12.19203 12.18573 12.17975
## [481] 12.17411 12.16882 12.16391 12.15938 12.15525 12.15156 12.14830 12.14551
## [489] 12.14319 12.14137 12.14006 12.13929 12.13906 12.13940 12.14033 12.14186
## [497] 12.14401 12.14728 12.15206 12.15824 12.16568 12.17426 12.18385 12.19433
## [505] 12.20558 12.21746 12.22985 12.24263 12.25567 12.26884 12.28202 12.29508
## [513] 12.30791 12.32036 12.33232 12.34365 12.35425 12.36397 12.37473 12.38832
## [521] 12.40443 12.42271 12.44287 12.46457 12.48750 12.51134 12.53576 12.56045
## [529] 12.58508 12.60934 12.63290 12.65544 12.67665 12.69621 12.71378 12.72906
## [537] 12.74172 12.75144 12.75791 12.76304 12.76891 12.77539 12.78234 12.78964
## [545] 12.79716 12.80478 12.81236 12.81979 12.82692 12.83364 12.83981 12.84531
## [553] 12.85001 12.85378 12.85650 12.85804 12.85826 12.85705 12.85427 12.84979
## [561] 12.84350 12.83525 12.82429 12.81012 12.79307 12.77345 12.75159 12.72779
## [569] 12.70239 12.67570 12.64803 12.61970 12.59104 12.56236 12.53397 12.50620
## [577] 12.47937 12.45378 12.42977 12.40765 12.38773 12.36569 12.33757 12.30423
## [585] 12.26654 12.22538 12.18161 12.13611 12.08974 12.04338 11.99790 11.95415
## [593] 11.91303 11.87539 11.84210 11.81404 11.79207 11.77372 11.75594 11.73875
## [601] 11.72216 11.70619 11.69087 11.67621 11.66222 11.64893 11.63636 11.62451
## [609] 11.61342 11.60309 11.59354 11.58480 11.57688 11.56980 11.56358 11.55823
## [617] 11.55361 11.54955 11.54607 11.54321 11.54097 11.53938 11.53846 11.53822
## [625] 11.53870 11.53990 11.54184 11.54456 11.54806 11.55237 11.55751 11.56349
## [633] 11.57023 11.57763 11.58569 11.59443 11.60385 11.61396 11.62478 11.63631
## [641] 11.64856 11.66153 11.67525 11.68972 11.70494 11.72093 11.73770 11.75525
## [649] 11.77360 11.79275 11.81272 11.83349 11.85504 11.87737 11.90048 11.92435
## [657] 11.94899 11.97438 12.00053 12.02742 12.05506 12.08343 12.11253 12.14235
## [665] 12.17290 12.20415 12.23612
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 667)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.59606 12.59293 12.58987 12.58690 12.58399 12.58117 12.57841 12.57572
##   [9] 12.57311 12.57056 12.56807 12.56565 12.56329 12.56099 12.55874 12.55656
##  [17] 12.55442 12.55234 12.55031 12.54833 12.54640 12.54451 12.54266 12.54086
##  [25] 12.53909 12.53737 12.53568 12.53403 12.53240 12.53081 12.52925 12.52772
##  [33] 12.52621 12.52472 12.52326 12.52182 12.52037 12.51887 12.51735 12.51580
##  [41] 12.51423 12.51264 12.51106 12.50947 12.50789 12.50632 12.50478 12.50327
##  [49] 12.50179 12.50035 12.49895 12.49762 12.49634 12.49514 12.49400 12.49295
##  [57] 12.49199 12.49112 12.49035 12.48970 12.48915 12.48873 12.48844 12.48828
##  [65] 12.48826 12.48839 12.48868 12.48913 12.48975 12.49054 12.49151 12.49261
##  [73] 12.49376 12.49496 12.49622 12.49755 12.49893 12.50038 12.50190 12.50348
##  [81] 12.50513 12.50686 12.50865 12.51052 12.51247 12.51450 12.51661 12.51879
##  [89] 12.52107 12.52343 12.52587 12.52841 12.53104 12.53376 12.53657 12.53949
##  [97] 12.54250 12.54561 12.54883 12.55215 12.55557 12.55910 12.56275 12.56650
## [105] 12.57037 12.57435 12.57815 12.58150 12.58445 12.58702 12.58928 12.59125
## [113] 12.59298 12.59452 12.59591 12.59719 12.59840 12.59958 12.60079 12.60205
## [121] 12.60342 12.60493 12.60664 12.60857 12.61078 12.61330 12.61619 12.61947
## [129] 12.62321 12.62743 12.63217 12.63750 12.64343 12.65088 12.66054 12.67223
## [137] 12.68572 12.70080 12.71726 12.73490 12.75349 12.77284 12.79273 12.81294
## [145] 12.83327 12.85351 12.87345 12.89287 12.91156 12.92932 12.94593 12.96118
## [153] 12.97487 12.98677 12.99975 13.01643 13.03625 13.05869 13.08318 13.10917
## [161] 13.13613 13.16350 13.19073 13.21727 13.24259 13.26612 13.28733 13.30566
## [169] 13.32056 13.33149 13.34038 13.34947 13.35870 13.36800 13.37729 13.38652
## [177] 13.39561 13.40449 13.41309 13.42135 13.42919 13.43655 13.44336 13.44955
## [185] 13.45505 13.45979 13.46370 13.46672 13.46877 13.46979 13.46970 13.46845
## [193] 13.46595 13.46215 13.45697 13.45034 13.44219 13.43247 13.42015 13.40450
## [201] 13.38585 13.36454 13.34088 13.31521 13.28787 13.25917 13.22945 13.19905
## [209] 13.16828 13.13748 13.10698 13.07711 13.04820 13.02057 12.99457 12.97051
## [217] 12.94874 12.92628 12.90022 12.87094 12.83882 12.80426 12.76765 12.72936
## [225] 12.68978 12.64930 12.60830 12.56718 12.52631 12.48609 12.44690 12.40912
## [233] 12.37315 12.33936 12.30815 12.27990 12.25499 12.23382 12.21456 12.19521
## [241] 12.17586 12.15661 12.13756 12.11879 12.10042 12.08252 12.06521 12.04857
## [249] 12.03270 12.01771 12.00367 11.99070 11.97889 11.96832 11.95998 11.95456
## [257] 11.95179 11.95136 11.95301 11.95643 11.96134 11.96746 11.97450 11.98216
## [265] 11.99017 11.99824 12.00607 12.01338 12.01988 12.02529 12.02933 12.03169
## [273] 12.03209 12.03361 12.03911 12.04800 12.05970 12.07362 12.08918 12.10579
## [281] 12.12287 12.13983 12.15609 12.17106 12.18415 12.19478 12.20237 12.20632
## [289] 12.20606 12.20302 12.19906 12.19425 12.18862 12.18225 12.17518 12.16747
## [297] 12.15918 12.15036 12.14106 12.13134 12.12125 12.11086 12.10020 12.08935
## [305] 12.07836 12.06727 12.05615 12.04505 12.03402 12.02313 12.01038 11.99405
## [313] 11.97455 11.95229 11.92771 11.90121 11.87322 11.84415 11.81441 11.78444
## [321] 11.75464 11.72543 11.69724 11.67047 11.64556 11.62290 11.60294 11.58607
## [329] 11.57273 11.55992 11.54469 11.52747 11.50870 11.48883 11.46828 11.44751
## [337] 11.42694 11.40702 11.38819 11.37088 11.35553 11.34259 11.33249 11.32568
## [345] 11.32258 11.32215 11.32303 11.32514 11.32845 11.33290 11.33843 11.34499
## [353] 11.35253 11.36100 11.37034 11.38050 11.39143 11.40307 11.41537 11.42828
## [361] 11.44175 11.45572 11.47013 11.48495 11.50179 11.52213 11.54567 11.57212
## [369] 11.60116 11.63250 11.66583 11.70087 11.73730 11.77482 11.81315 11.85196
## [377] 11.89097 11.92988 11.96838 12.00617 12.04295 12.07843 12.11229 12.14425
## [385] 12.17399 12.20123 12.22565 12.24963 12.27557 12.30319 12.33223 12.36241
## [393] 12.39347 12.42514 12.45716 12.48924 12.52113 12.55256 12.58325 12.61294
## [401] 12.64136 12.66824 12.69331 12.71631 12.73696 12.75500 12.77209 12.78997
## [409] 12.80841 12.82722 12.84620 12.86513 12.88381 12.90204 12.91961 12.93633
## [417] 12.95197 12.96635 12.97925 12.99047 12.99981 13.00706 13.01221 13.01550
## [425] 13.01705 13.01698 13.01544 13.01255 13.00843 13.00323 12.99707 12.99008
## [433] 12.98239 12.97413 12.96543 12.95642 12.94723 12.93799 12.92883 12.91989
## [441] 12.91128 12.90135 12.88854 12.87312 12.85536 12.83553 12.81391 12.79078
## [449] 12.76640 12.74105 12.71501 12.68855 12.66194 12.63546 12.60938 12.58397
## [457] 12.55951 12.53627 12.51453 12.49456 12.47663 12.46101 12.44517 12.42665
## [465] 12.40590 12.38336 12.35947 12.33467 12.30940 12.28411 12.25923 12.23521
## [473] 12.21248 12.19149 12.17268 12.15649 12.14336 12.13373 12.12565 12.11694
## [481] 12.10775 12.09823 12.08852 12.07876 12.06910 12.05968 12.05065 12.04214
## [489] 12.03431 12.02730 12.02124 12.01629 12.01259 12.01028 12.00950 12.01041
## [497] 12.01313 12.01811 12.02552 12.03516 12.04684 12.06033 12.07545 12.09198
## [505] 12.10973 12.12848 12.14803 12.16818 12.18873 12.20946 12.23018 12.25069
## [513] 12.27076 12.29021 12.30883 12.32641 12.34274 12.35764 12.37401 12.39463
## [521] 12.41904 12.44676 12.47734 12.51032 12.54523 12.58161 12.61900 12.65694
## [529] 12.69497 12.73262 12.76943 12.80494 12.83868 12.87021 12.89904 12.92473
## [537] 12.94681 12.96481 12.97828 12.98959 13.00133 13.01338 13.02562 13.03794
## [545] 13.05021 13.06232 13.07414 13.08556 13.09645 13.10670 13.11618 13.12478
## [553] 13.13238 13.13885 13.14408 13.14795 13.15034 13.15112 13.15019 13.14741
## [561] 13.14267 13.13585 13.12638 13.11393 13.09877 13.08118 13.06144 13.03981
## [569] 13.01656 12.99196 12.96630 12.93983 12.91284 12.88558 12.85835 12.83140
## [577] 12.80501 12.77945 12.75499 12.73190 12.71046 12.68675 12.65721 12.62265
## [585] 12.58387 12.54168 12.49690 12.45032 12.40276 12.35503 12.30793 12.26228
## [593] 12.21887 12.17853 12.14205 12.11024 12.08392 12.06016 12.03563 12.01048
## [601] 11.98490 11.95907 11.93314 11.90730 11.88172 11.85657 11.83202 11.80825
## [609] 11.78542 11.76372 11.74332 11.72438 11.70708 11.69160 11.67810 11.66676
## [617] 11.65665 11.64678 11.63721 11.62802 11.61929 11.61110 11.60352 11.59662
## [625] 11.59048 11.58517 11.58078 11.57737 11.57502 11.57381 11.57381 11.57510
## [633] 11.57738 11.58030 11.58390 11.58820 11.59320 11.59895 11.60545 11.61273
## [641] 11.62081 11.62970 11.63944 11.65005 11.66153 11.67392 11.68724 11.70150
## [649] 11.71673 11.73295 11.75017 11.76848 11.78788 11.80837 11.82991 11.85248
## [657] 11.87606 11.90062 11.92613 11.95258 11.97993 12.00815 12.03724 12.06715
## [665] 12.09787 12.12936 12.16162
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 667)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.93734 11.93639 11.93545 11.93454 11.93364 11.93276 11.93188 11.93101
##   [9] 11.93015 11.92929 11.92842 11.92755 11.92667 11.92578 11.92487 11.92395
##  [17] 11.92300 11.92204 11.92104 11.92001 11.91895 11.91786 11.91672 11.91554
##  [25] 11.91432 11.91304 11.91172 11.91034 11.90890 11.90740 11.90583 11.90420
##  [33] 11.90250 11.90072 11.89887 11.89693 11.89492 11.89281 11.89062 11.88834
##  [41] 11.88595 11.88348 11.88090 11.87815 11.87518 11.87202 11.86866 11.86514
##  [49] 11.86145 11.85763 11.85369 11.84964 11.84549 11.84127 11.83699 11.83266
##  [57] 11.82830 11.82393 11.81956 11.81521 11.81089 11.80662 11.80241 11.79829
##  [65] 11.79426 11.79034 11.78655 11.78290 11.77941 11.77610 11.77298 11.77006
##  [73] 11.76736 11.76491 11.76270 11.76076 11.75911 11.75776 11.75613 11.75365
##  [81] 11.75040 11.74643 11.74180 11.73657 11.73080 11.72456 11.71789 11.71088
##  [89] 11.70356 11.69601 11.68828 11.68044 11.67254 11.66465 11.65682 11.64912
##  [97] 11.64160 11.63433 11.62736 11.62076 11.61459 11.60890 11.60376 11.59923
## [105] 11.59537 11.59224 11.58989 11.58839 11.58781 11.58819 11.58960 11.59210
## [113] 11.59575 11.60071 11.60703 11.61462 11.62337 11.63319 11.64399 11.65566
## [121] 11.66810 11.68123 11.69493 11.70911 11.72369 11.73854 11.75359 11.76873
## [129] 11.78386 11.79889 11.81372 11.82824 11.84237 11.85601 11.86905 11.88349
## [137] 11.90116 11.92175 11.94496 11.97046 11.99795 12.02712 12.05764 12.08922
## [145] 12.12154 12.15428 12.18713 12.21978 12.25192 12.28324 12.31342 12.34216
## [153] 12.36913 12.39403 12.41655 12.43636 12.45598 12.47791 12.50182 12.52741
## [161] 12.55437 12.58238 12.61115 12.64034 12.66966 12.69880 12.72743 12.75526
## [169] 12.78196 12.80723 12.83076 12.85224 12.87134 12.88778 12.90122 12.91314
## [177] 12.92514 12.93717 12.94915 12.96102 12.97270 12.98413 12.99523 13.00594
## [185] 13.01619 13.02591 13.03503 13.04348 13.05119 13.05809 13.06412 13.06920
## [193] 13.07327 13.07626 13.07809 13.07870 13.07802 13.07598 13.07252 13.06755
## [201] 13.06102 13.05285 13.04297 13.03132 13.01602 12.99568 12.97099 12.94262
## [209] 12.91126 12.87758 12.84228 12.80602 12.76950 12.73338 12.69835 12.66510
## [217] 12.63429 12.60662 12.58277 12.55887 12.53093 12.49937 12.46463 12.42714
## [225] 12.38734 12.34566 12.30252 12.25837 12.21365 12.16877 12.12418 12.08030
## [233] 12.03758 11.99645 11.95733 11.92067 11.88689 11.85643 11.82973 11.80721
## [241] 11.78695 11.76676 11.74669 11.72679 11.70712 11.68771 11.66862 11.64991
## [249] 11.63161 11.61378 11.59646 11.57972 11.56358 11.54812 11.53337 11.51939
## [257] 11.50622 11.49391 11.48251 11.47355 11.46820 11.46601 11.46650 11.46923
## [265] 11.47373 11.47953 11.48619 11.49324 11.50021 11.50666 11.51211 11.51612
## [273] 11.51821 11.51793 11.51481 11.51187 11.51214 11.51523 11.52077 11.52835
## [281] 11.53758 11.54808 11.55946 11.57132 11.58327 11.59492 11.60589 11.61578
## [289] 11.62420 11.63076 11.63507 11.63674 11.63538 11.63060 11.62224 11.61067
## [297] 11.59632 11.57963 11.56102 11.54092 11.51976 11.49797 11.47598 11.45422
## [305] 11.43310 11.41308 11.39456 11.37799 11.36378 11.35238 11.34046 11.32473
## [313] 11.30566 11.28374 11.25942 11.23318 11.20549 11.17682 11.14765 11.11844
## [321] 11.08967 11.06181 11.03532 11.01069 10.98837 10.96885 10.95260 10.94008
## [329] 10.93177 10.92599 10.92076 10.91611 10.91208 10.90868 10.90595 10.90391
## [337] 10.90260 10.90204 10.90226 10.90329 10.90516 10.90789 10.91151 10.91606
## [345] 10.92156 10.92877 10.93830 10.94997 10.96356 10.97889 10.99575 11.01394
## [353] 11.03327 11.05353 11.07454 11.09608 11.11797 11.13999 11.16196 11.18368
## [361] 11.20495 11.22556 11.24532 11.26404 11.28327 11.30459 11.32780 11.35274
## [369] 11.37920 11.40701 11.43599 11.46593 11.49667 11.52800 11.55976 11.59175
## [377] 11.62379 11.65569 11.68727 11.71834 11.74871 11.77821 11.80664 11.83383
## [385] 11.85957 11.88370 11.90602 11.92812 11.95156 11.97615 12.00169 12.02798
## [393] 12.05482 12.08201 12.10935 12.13665 12.16369 12.19029 12.21624 12.24135
## [401] 12.26541 12.28823 12.30961 12.32934 12.34723 12.36307 12.37840 12.39471
## [409] 12.41177 12.42936 12.44726 12.46524 12.48308 12.50056 12.51744 12.53351
## [417] 12.54854 12.56231 12.57459 12.58516 12.59380 12.60027 12.60435 12.60609
## [425] 12.60570 12.60339 12.59938 12.59387 12.58709 12.57924 12.57053 12.56118
## [433] 12.55140 12.54141 12.53141 12.52161 12.51224 12.50350 12.49560 12.48876
## [441] 12.48319 12.47724 12.46927 12.45946 12.44799 12.43506 12.42087 12.40558
## [449] 12.38940 12.37252 12.35512 12.33740 12.31953 12.30172 12.28415 12.26701
## [457] 12.25048 12.23477 12.22005 12.20651 12.19436 12.18376 12.17352 12.16237
## [465] 12.15048 12.13797 12.12499 12.11170 12.09823 12.08473 12.07135 12.05822
## [473] 12.04550 12.03332 12.02184 12.01119 12.00153 11.99299 11.98457 11.97527
## [481] 11.96523 11.95462 11.94356 11.93223 11.92075 11.90929 11.89800 11.88702
## [489] 11.87650 11.86659 11.85745 11.84922 11.84205 11.83609 11.83150 11.82841
## [497] 11.82698 11.82673 11.82705 11.82794 11.82939 11.83138 11.83390 11.83695
## [505] 11.84050 11.84455 11.84908 11.85409 11.85956 11.86548 11.87184 11.87862
## [513] 11.88583 11.89343 11.90143 11.90980 11.91855 11.92765 11.93857 11.95257
## [521] 11.96931 11.98849 12.00978 12.03287 12.05743 12.08315 12.10970 12.13678
## [529] 12.16405 12.19120 12.21791 12.24387 12.26874 12.29222 12.31398 12.33371
## [537] 12.35108 12.36578 12.37748 12.38863 12.40174 12.41657 12.43289 12.45047
## [545] 12.46907 12.48846 12.50841 12.52869 12.54906 12.56930 12.58916 12.60842
## [553] 12.62685 12.64421 12.66026 12.67479 12.68755 12.69832 12.70685 12.71292
## [561] 12.71630 12.71675 12.71430 12.70930 12.70198 12.69257 12.68132 12.66845
## [569] 12.65421 12.63883 12.62255 12.60561 12.58823 12.57065 12.55312 12.53587
## [577] 12.51913 12.50314 12.48814 12.47436 12.46204 12.44792 12.42900 12.40594
## [585] 12.37937 12.34995 12.31831 12.28511 12.25098 12.21657 12.18253 12.14950
## [593] 12.11812 12.08904 12.06290 12.04036 12.02204 12.00559 11.98830 11.97033
## [601] 11.95184 11.93299 11.91394 11.89484 11.87585 11.85714 11.83885 11.82116
## [609] 11.80421 11.78817 11.77320 11.75944 11.74707 11.73624 11.72711 11.71984
## [617] 11.71356 11.70739 11.70137 11.69560 11.69015 11.68509 11.68049 11.67644
## [625] 11.67300 11.67024 11.66826 11.66711 11.66687 11.66762 11.66943 11.67238
## [633] 11.67620 11.68059 11.68556 11.69111 11.69727 11.70404 11.71143 11.71946
## [641] 11.72813 11.73746 11.74746 11.75813 11.76950 11.78157 11.79435 11.80785
## [649] 11.82209 11.83708 11.85282 11.86937 11.88676 11.90498 11.92401 11.94382
## [657] 11.96442 11.98576 12.00785 12.03067 12.05419 12.07840 12.10328 12.12882
## [665] 12.15500 12.18180 12.20921
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")